from itsdangerous import BadSignature
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer

from meiduo_market.settings import SECRET_KEY


def generic_openid(openid):
    s = Serializer(secret_key=SECRET_KEY,expires_in=600)
    access_token = s.dumps({'openid':openid})
    return access_token.decode()

def decode_openid(access_token):
    s = Serializer(secret_key=SECRET_KEY, expires_in=600)
    try:
        result = s.loads(access_token)
    except BadSignature:
        return None

    return result.get('openid')